<?php

namespace App\Console\Commands;

use App\Models\Currency;
use App\Models\CurrencyLog;
use App\Models\Recharge;
use App\Models\RechOrder;
use Illuminate\Console\Command;
use App\Traits\YunXinTrait;
use App\Traits\HandlePriceTrait;
class OrdersSocket extends Command
{
    use YunXinTrait, HandlePriceTrait;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'socket:orders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'order socket';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders1 = RechOrder::where(['type'=> 1])->get();
        $orders2 = RechOrder::where(['type'=> 2])->get();
        $btc = [];
        $map = [
            'type'=>1
        ];
        foreach ($orders1 as $k=>$order) {
            $data = [];
            $bg_time = date("Y-m-d H:i:s", strtotime("-30 day"));
            $end_time = date("Y-m-d H:i:s", time());
            $type1[$k]['nickname'] = $order->user->nickname;//名称
            $type1[$k]['photo'] = $order->user->headimg;//图片
            $type1[$k]['date_num'] = Recharge::where(['order_id'=>$order->id,'status'=>6])->whereBetween('created_at', [$bg_time, $end_time])->value('num')??0;//近30日销售数量
            $type1[$k]['num'] = number_format($order->surplus_num,6)??0;//剩余数量
            $type1[$k]['price'] = $order->price;//单价
            $type1[$k]['min'] = $order->mix??0;//最小限额
            $type1[$k]['max'] = $order->max??0;//最大限额
            $type1[$k]['id'] = $order->id;//订单id
            $type1[$k]['uid'] = $order->uid;//商家id
            $type1[$k]['type'] = $order->type;//买卖类型
                //支付信息
//            $type1[$k]['pay_info'] = $order->trade_model;
            $type1[$k]['pay_info'] = $order->trade_model;
            $real = explode(',',$type1[$k]['pay_info']);
            $type1[$k]['alipay'] = $type1[$k]['wechat'] = $type1[$k]['bank'] = null;
            foreach ($real as $v){
                switch ($v){
                    case 1;
                        $type1[$k]['wechat'] = $order->user->user_wechat()->select('wechat_account', 'wechat_code')->first();

                        break;
                    case 2;
                        $type1[$k]['alipay'] = $order->user->user_alipay()->select('alipay_account', 'alipay_code')->first();

                        break;
                    case 3;
                        $type1[$k]['bank'] = $order->user->user_bank()->select('bank_no', 'bank_name', 'bank_info')->first();
                        break;
                }
            }
//            $type1[$k]['alipay'] = $order->user->user_alipay()->select('alipay_account')->first();
//            $type1[$k]['wechat'] = $order->user->user_wechat()->select('wechat_account')->first();
//            $type1[$k]['bank'] = $order->user->user_bank()->select('bank_no')->first();
        }
        array_push($btc, $type1);

        foreach ($orders2 as $key=>$v) {
            $data = [];
            $bg_time = date("Y-m-d H:i:s", strtotime("-30 day"));
            $end_time = date("Y-m-d H:i:s", time());
            $type2[$key]['nickname'] = $v->user->nickname;//名称
            $type2[$key]['photo'] = $v->user->headimg;//图片
            $type2[$key]['date_num'] = Recharge::where(['order_id'=>$v->id,'status'=>6])->whereBetween('created_at', [$bg_time, $end_time])->value('num')??0;//近30日销售数量
            $type2[$key]['num'] = number_format($v->surplus_num,6)??0;//剩余数量
            $type2[$key]['price'] = $v->price;//单价
            $type2[$key]['min'] = $v->mix??0;//最小限额
            $type2[$key]['max'] = $v->max??0;//最大限额
            $type2[$key]['id'] = $v->id;//订单id
            $type2[$key]['uid'] = $v->uid;//商家id
            $type2[$key]['type'] = $v->type;//买卖类型
            //支付信息
//            $type2[$key]['pay_info'] = $v->trade_model;
            $type2[$key]['pay_info'] = $v->trade_model;
            $real1 = explode(',',$type2[$key]['pay_info']);
            $type2[$key]['alipay'] = $type2[$key]['wechat'] = $type2[$key]['bank'] = null;
            foreach ($real1 as $v1){
                switch ($v1){
                    case 1;
                        $type2[$key]['wechat'] = $v->user->user_wechat()->select('wechat_account', 'wechat_code')->first();

                        break;
                    case 2;
                        $type2[$key]['alipay'] = $v->user->user_alipay()->select('alipay_account', 'alipay_code')->first();

                        break;
                    case 3;
                        $type2[$key]['bank'] = $v->user->user_bank()->select('bank_no', 'bank_name', 'bank_info')->first();
                        break;
                }
            }
//            $type2[$key]['alipay'] = $v->user->user_alipay()->select('alipay_account')->first();
//            $type2[$key]['wechat'] = $v->user->user_wechat()->select('wechat_account')->first();
//            $type2[$key]['bank'] = $v->user->user_bank()->select('bank_no')->first();

        }
        array_push($btc, $type2);

        $type    = 'publish';
        //$content = '{"type":1,"content":"3333"}';
        /*$content = [
            'type'=>1,
            'content'=> json_encode($btc),
        ];*/
        $content = json_encode($btc);
        $uid = 'entryorder_list';
        $push_api_url = "http://114.215.40.82:2121/";
        //$push_api_url = "http://sj1.51tuanli.com/";
        $post_data = [
            "type" => $type,
            "content" => $content,
            "to" => $uid,
        ];
        //dd($post_data);
        //$post_params = http_build_query($post_data);
        //$push_api_url = "http://114.215.40.82:2121/?".$post_params;

        dump($push_api_url);

        $ch = curl_init ();

        curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
        //dd($ch);
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        dump($return);
    }
}
